package com.qdlc.p2p.quartz.job;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import com.google.common.base.Joiner;
import com.qd.p2p.adapter.AdaptedRequestHandleService;
import com.qd.p2p.adapter.param.QueryP2PBusinessParameter;
import com.qd.p2p.adapter.response.QueryP2PBusinessUserInfoResponse;
import com.qd.p2p.baofoo.enums.P2pBizType;
import com.qdlc.p2p.biz.service.UserIdentifyService;
import com.qdlc.p2p.common.util.CollectionUtils;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.UserIdentifyModel;
import com.qdlc.p2p.dal.util.UserUtils;
import com.qdlc.p2p.quartz.AbstractJob;

/**
 * Created by gaoj on 2016/5/11.
 */
public class RealNameStatusRefresh extends AbstractJob {

    @Resource
    AdaptedRequestHandleService adaptedRequestHandleService;

    @Resource
    UserIdentifyService userIdentifyService;

    @Override
    protected void process() throws Exception {

        //查询realNameStatus=2状态的tppUserCustId，并将映射关系put进Map集合
        UserIdentifyModel model = new UserIdentifyModel();
        model.setRealNameStatus(2);
        model.setVipStatus(-2);
        model.setEmailStatus(-2);
        model.setMobilePhoneStatus(-2);
        model.setPage(1);
        model.setRows(100);
        PageDataList<UserIdentifyModel> pageDataList = userIdentifyService.findUserIdentifylist(model);
        List<UserIdentifyModel> userIdentifyModelList = pageDataList.getList();
        if (!CollectionUtils.isNotNullOrEmpty(userIdentifyModelList)) {
            return;
        }

        Set<String> tppUserIdSet = new HashSet<String>();
        Map<String, UserIdentifyModel> userIdentifyModellMap = new HashMap<String, UserIdentifyModel>();
        for (UserIdentifyModel userIdentifyModel : userIdentifyModelList) {
            long userId = userIdentifyModel.getUserId();
            User user = UserUtils.getUserById(userId);
            String tppUserCustId = user.getTppUserCustId();
            tppUserIdSet.add(tppUserCustId);
            userIdentifyModellMap.put(tppUserCustId, userIdentifyModel);
        }
        String tppUserIds = Joiner.on(',').join(tppUserIdSet);  //将tppUserCustId用','间隔开

        //设置请求参数，请求业务查询接口,查询用户实名认证状态
        QueryP2PBusinessParameter parameter = new QueryP2PBusinessParameter();
        parameter.setType(P2pBizType.USER.getType());
        parameter.setOrderId(tppUserIds);  //用户信息查询,传递值改为tppUserIds
        QueryP2PBusinessUserInfoResponse response = adaptedRequestHandleService.queryP2PBusinessUserInfo(parameter);
        List<QueryP2PBusinessUserInfoResponse.UserInfo> userInfos = response.getResult();
        if (userInfos == null) {
            return;
        }
        //如果返回的结果不为空的话，就遍历集合获取用户在第三方平台的认证状态，如果第三方平台显示已经认证那么本平台状态也改为已认证
        for (QueryP2PBusinessUserInfoResponse.UserInfo userInfo : userInfos) {
            int state = userInfo.getState();
            String platformUserId = userInfo.getPlatformUserId();
            User user = UserUtils.getUserByTppCustId(platformUserId);
            long userId = user.getUserId();
            UserIdentifyModel userIdentifyModel = userIdentifyModellMap.get(platformUserId);
            if (state != userIdentifyModel.getRealNameStatus()) {
                userIdentifyService.modifyRealnameStatus(userId, state, userIdentifyModel.getRealNameStatus());
            }
        }
    }
}

